<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" errorPage="error.jsp"
    %>
<%@ page import="java.io.*,java.util.*" %>
<%@ page import="java.net.URLEncoder" %>	
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%
String audioPath = "temp";
	//the path to uploaded
	File uploadPath = new File(request.getSession().getServletContext().getRealPath("")+"\\temp\\");
	if(!uploadPath.exists()){
		uploadPath.mkdirs();
	}
	
	//temp folder
	File tempPathFile = new File(request.getSession().getServletContext().getRealPath("")+"\\temp\\buffer\\");
	if(!tempPathFile.exists()){
		tempPathFile.mkdirs();
	}
	
	//check if the request is of multipart form request
	boolean isMultipart = ServletFileUpload.isMultipartContent(request);
	if(isMultipart){
		try{
			//create diskfilefactory object for this request
			DiskFileItemFactory factory = new DiskFileItemFactory();
			
			//set factory constrains
			factory.setSizeThreshold(8192);  //buffer size, 4kb here
			factory.setRepository(tempPathFile);  //buffer path
			
			//create a new file upload handler
			ServletFileUpload upload = new ServletFileUpload(factory);
			
			//set overall request size constraint
			upload.setSizeMax(1024*1024*4);  //set max file size, 4mb here
			
			List<FileItem> items = upload.parseRequest(request);
			Iterator<FileItem> itr = items.iterator();
			while(itr.hasNext()){
				FileItem item = (FileItem)itr.next();
				String fileName = item.getName();
				if(fileName != null){
					File fullFile = new File(item.getName());
					if(!item.getName().toLowerCase().endsWith(".jpg") &&
							!item.getName().toLowerCase().endsWith(".jpeg") &&
							!item.getName().toLowerCase().endsWith(".png") &&
							!item.getName().toLowerCase().endsWith(".tiff") &&
							!item.getName().toLowerCase().endsWith(".raw") &&
							!item.getName().toLowerCase().endsWith(".bmp") &&
							!item.getName().toLowerCase().endsWith(".gif") &&
							!item.getName().toLowerCase().endsWith(".wav")){
						out.print("<script>alert('请确认所选文件格式为是否正确!');location.href='queryentry.jsp';</script>");
						break;
					}
					File savedFile = new File(uploadPath, URLEncoder.encode(fullFile.getName()));
					item.write(savedFile);
					
					session.setAttribute("uploadedfile", uploadPath+"\\"+URLEncoder.encode(fullFile.getName()));
					if(item.getName().toLowerCase().endsWith(".wav")){
						session.setAttribute("videoretrieval", "false");
						session.setAttribute("audioretrieval", "true");
					}else{//if(item.getName().toLowerCase().endsWith(".jpg")){
						session.setAttribute("videoretrieval", "true");
						session.setAttribute("audioretrieval", "false");
					} 
					out.print("<script>window.parent.frames.item('resultlist').location.reload();</script>");
					break;
				}
			}
			out.print("<script>location.href='queryentry.jsp';</script>");
		}catch(Exception e){
			e.printStackTrace();
			out.print("<script>alert('please select file!');location.href='queryentry.jsp';</script>");
		}	
	}else{
		out.print("<script>alert('the enctype must be multipart/form-data!');location.href='queryentry.jsp';</script>");
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Upload and Retrieval</title>
</head>
<body>

</body>
</html>